package com.somsoft.handjc.activity.setting;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.somsoft.handjc.activity.R;
import com.somsoft.handjc.util.BaseActivity;
import com.somsoft.handjc.util.MD5;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.Utils;

public class ChangePwdActivity extends BaseActivity {
	EditText pwd1 = null;
	EditText pwd2 = null;
	Button btnOk = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.changepwd);
		setTitle("修改密码");
		pwd1 = (EditText)findViewById(R.id.changepwd_pwd1);
		pwd2 = (EditText)findViewById(R.id.changepwd_pwd2);
		btnOk = (Button)findViewById(R.id.changepwd_button);
		btnOk.setOnClickListener(new OnClickListenerImpl());
	}
	
	private class OnClickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
			try {
				if("".equals(pwd1.getText().toString()) || "".equals(pwd2.getText().toString())){
					Toast.makeText(ChangePwdActivity.this, "新密码不能为空！", Toast.LENGTH_SHORT).show();
				}else if(!pwd1.getText().toString().equals(pwd2.getText().toString())){
					Toast.makeText(ChangePwdActivity.this, "两次新密码不相同！", Toast.LENGTH_SHORT).show();
				}else{
					List <NameValuePair> params=new ArrayList<NameValuePair>();   
					params.add(new BasicNameValuePair("username",Utils.LOGIN_USER));   
					params.add(new BasicNameValuePair("password",new MD5().getMD5ofStr(pwd1.getText().toString())));   
					String result = Utils.getJsonData(Utils.SERVER_IP + "/handweb/system/system!changePwd.hand", params);
					if("true".equals(result)){
						Toast.makeText(ChangePwdActivity.this, "密码修改成功！", Toast.LENGTH_SHORT).show();
						pwd1.setText("");
						pwd2.setText("");
					}else{
						Toast.makeText(ChangePwdActivity.this, "密码修改失败！", Toast.LENGTH_SHORT).show();
					}
				}
			} catch (Exception e) {
				Toast.makeText(ChangePwdActivity.this, "密码修改失败！", Toast.LENGTH_SHORT).show();
			}
		}
	}
	

}